<template>
	<view>
		<view class="index-iamges">
			<image class="index-img" :style="'width: ' + logowidth + 'px; height: ' + logoheight + 'px;'"
				:src="school.logo" @load="imageLoadLogo"></image>
		</view>
		<view class="weather">
			<text>{{weatherData}}</text>
		</view>
		<view class="school-images">
			<image v-for="(item, index) in school.images" :key="index" style="width: 100%;" mode="widthFix"
				:src="item.image" class="school-image"></image>
			<!-- <image class="index-img"  style="width: {{imagewidth}}px;" src="{{item.image}}" 
    bindload="imageLoad">
  </image> -->
		</view>
	</view>
</template>

<script>
	// page/index/index.js
	var imageUtil = require("../../utils/util.js");
	var bmap = require("../../utils/bmap-wx.min.js");
	var app = getApp();
	var schoolId = 0;

	export default {
		data() {
			return {
				school: {
					logo: "/static/image/kx-logo.png",
					images: []
				},
				logowidth: 0,
				//缩放后的宽
				logoheight: 0,
				//缩放后的高,
				images: [],
				imagewidth: 0,
				imageheight: 0,
				weatherData: '',
				title: '',
				user: false,
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(opt) {
			console.log('opt', opt);
			this.user = uni.getStorageSync('user')
			let sessionSid = uni.getStorageSync('schoolId')
			if (opt.sid) {
				schoolId = parseInt(opt.sid)
				uni.setStorageSync('schoolid', schoolId)
			} else if (sessionSid > 0) {
				schoolId = sessionSid
			} else if (this.user) {
				schoolId = this.user.school_id
				uni.setStorageSync('schoolid', schoolId)
			}

			this.baiduWeather();
			this.getSchoolInfo();
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let fresh = uni.getStorageSync('fresh')
			if (fresh) {
				this.user = uni.getStorageSync('user')
				if (this.user) {
					schoolId = this.user.school_id
					uni.setStorageSync('schoolid', schoolId)
				}
				uni.setStorageSync('fresh', 0)
				this.baiduWeather();
				this.getSchoolInfo();
			}
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			return {
				title: '科小智控',
				path: '/page/index?sid=' + schoolId
			};
		},
		methods: {
			baiduWeather: function(e) {
				var BMap = new bmap.BMapWX({
					ak: 'nTsZroAu7TqmaKqUDlrtGVKbop2A9RNN'
				});

				var fail = function(data) {
					console.log('fail!!!!', fail);
				};

				var success = function(data) {
					console.log('success!!!');
					var weatherData = data.currentWeather[0];
					weatherData = weatherData.date + '\n温度: ' + weatherData.temperature + '  天气: ' + weatherData
						.weatherDesc + '  风力: ' + weatherData.wind +
						'\n'; //weatherData = '城市：' + weatherData.currentCity + '\n' + 'PM2.5：' + weatherData.pm25 + '\n' + '日期：' + weatherData.date + '\n' + '温度：' + weatherData.temperature + '\n' + '天气：' + weatherData.weatherDesc + '\n' + '风力：' + weatherData.wind + '\n';

					that.setData({
						weatherData: weatherData
					});
				};

				BMap.weather({
					fail: fail,
					success: success
				});
			},
			imageLoadLogo: function(e) {
				var imageSize = imageUtil.imageUtil(e);
				this.setData({
					logowidth: imageSize.imageWidth,
					logoheight: imageSize.imageHeight
				});
			},
			imageLoad: function(e) {
				var imageSize = imageUtil.imageUtil(e);
				this.setData({
					imagewidth: imageSize.imageWidth,
					imageheight: imageSize.imageHeight
				});
			},
			indexData: function() {
				console.log('indexData', schoolId);
			},
			getSchoolInfo: function(sid = 0) {
				if (sid == 0) {
					sid = parseInt(schoolId)
				}
				if (sid > 0) {
					let that = this;
					app.globalData.JsonPost("api/pub/school_info", {
						sid: sid
					}, function(res) {
						if (res.state) {
							that.setData({
								school: res.data
							}); //that.school = res.data;

							console.log('schoolInfo', that.school);
						} else {
							console.log('schoolInfo err', res);
						}
					});
				}

			}
		}
	};
</script>
<style>
	.img {
		display: block;
		height: 500px;
	}

	.index-logo {
		background-color: white;
		text-align: center;
	}

	.index-logo image {
		width: 375px;
		margin: 0 auto;
		display: block;
		max-width: 95%;
	}

	.index-img {
		margin: 0;
		padding: 0
	}

	.weather {
		width: 100%;
		text-align: center;
		line-height: 22px;
		color: rgb(59, 57, 57);
		font-weight: 300;
		height: 100%;
		font-size: 14px;
		background-color: white;
		padding-bottom: 5px;
		margin-top: -10px;
	}

	.school-images {
		padding: 0;
	}

	image.school-image {
		margin-top: 0;
		margin-bottom: 0;
		padding: 0;
		display: flex;
	}
</style>
